﻿using System;
using System.Windows.Forms;

namespace MobileCommissionManagement.VodafoneControls
{
    public partial class VodafoneDealerImport : Form
    {
        private int _dealerSetupId;
        private string _fileName;
        private DateTime _yearMonth;

        #region Properties

        public VodafoneController VodafoneController
        {
            set { dealerSetupLookUpEdit.Properties.DataSource = value.ActiveDealerSetups; }
        }

        public int DealerSetupId
        {
            get { return _dealerSetupId; }
        }

        public string FileName
        {
            get { return _fileName; }
        }

        public DateTime YearMonth
        {
            get { return _yearMonth; }
        }

        #endregion

        public VodafoneDealerImport()
        {
            InitializeComponent();
            searchSimpleButton.Click += searchSimpleButton_Click;
            okSimpleButton.Click += okSimpleButton_Click;
        }

        private bool CanImport()
        {
            string errorMessage = string.Empty;
            if (dealerSetupLookUpEdit.EditValue == null)
                errorMessage = "Por favor, indique a configuração.";
            if (fileTextEdit.Text == string.Empty)
                errorMessage = "Por favor, indique a ficheiro.";
            if (dateEdit.EditValue == null)
                errorMessage = "Por favor, indique a data.";
            if(errorMessage != string.Empty)
                MessageBox.Show(errorMessage, "Importar Dealer Vodafone", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return errorMessage == string.Empty;
        }

        void okSimpleButton_Click(object sender, EventArgs e)
        {
            if (!CanImport())
            {
                DialogResult = DialogResult.None;
                return;
            }
            _dealerSetupId = (int)dealerSetupLookUpEdit.EditValue;
            _fileName = fileTextEdit.Text;
            _yearMonth = (DateTime)dateEdit.EditValue;            
        }

        void searchSimpleButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Text files (*.csv)|*.csv";
            openFileDialog.FilterIndex = 1;
            openFileDialog.RestoreDirectory = true;
            openFileDialog.AddExtension = true;
            openFileDialog.CheckFileExists = true;
            if (openFileDialog.ShowDialog(null) != DialogResult.OK)
                return;
            fileTextEdit.Text = openFileDialog.FileName;            
        }
        
    }
}
